//
//  WebViewController.swift
//  MyNavigationbar
//
//  Created by teleframe on 15/8/26.
//  Copyright (c) 2015年 tf. All rights reserved.
//

import UIKit

class WebViewController: UIViewController,UIWebViewDelegate,UITextFieldDelegate {
    
    @IBOutlet var progress: UIProgressView!
    @IBOutlet var textUrl: UITextField!
    var webView: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        progress.progress = 0
        progress.hidden = true
        webView = UIWebView(frame: CGRect(x: 0, y: 120, width: self.view.frame.size.width, height: self.view.frame.size.height-120))
        webView.delegate = self
        self.view.addSubview(webView)
        // Do any additional setup after loading the view.
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        loadUrl(textField.text, web: webView)
        textField.resignFirstResponder()
        return true
    }
    
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        textUrl.resignFirstResponder()
    }
    
    func loadUrl(url:String,web:UIWebView){
        if  url.isEmpty{
         return
        }
        let  aurl = NSURL(string: ("http://\(url)"))
        let urlRq = NSURLRequest(URL: aurl!)
        webView.loadRequest(urlRq)
        
    }
    
    func webViewDidStartLoad(web: UIWebView) {
        progress.hidden = false
        progress.progress = 0.5
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        progress.progress = 1
        progress.hidden = true
        UIApplication.sharedApplication().networkActivityIndicatorVisible = false
    }
    
    func webView(webView: UIWebView, didFailLoadWithError error: NSError) {
        progress.hidden = true
        UIApplication.sharedApplication().networkActivityIndicatorVisible = false
        webView.stopLoading()
    }
    
    
    
    /*
    // MARK: - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    }
    */
    
}
